package com.jerry.ReadWriteLock;

public class ReadWriteLockImpl implements ReadWriteLock {

    private final Object mutex = new Object();

    private int waitingWriter = 0;

    private int readingReader = 0;

    private int writingWriter = 0;

    private  Boolean preferWrite;

    public  ReadWriteLockImpl(Boolean preferWrite) {
         this.preferWrite = preferWrite;
    }

    public ReadLock getReadLock() {

        return new ReadLock(this);
    }

    public WriteLock getWriteLock() {


        return new WriteLock(this);
    }

    public int getWaitingWriter() {
        return waitingWriter;
    }

    @Override
    public int getReadingReader() {
        return readingReader;
    }

    @Override
    public int getWritingWriter() {
        return writingWriter;
    }

    public void increaseReadingReader() {

        readingReader++;
    }

    public void increaseWaitingWriter() {

        waitingWriter++;
    }

    public void increaseWritingWriter() {

        writingWriter++;
    }

    public void decreaseWaitingWriter() {

        waitingWriter--;

    }

    public void decreaseWritingWriter() {
        writingWriter--;

    }

    public void decreaseReadingReader() {

        readingReader--;

    }

    public Object getMutex() {
        return mutex;
    }

    public Boolean getPreferWrite() {
        return preferWrite;
    }

    @Override
    public void changePrefer(Boolean ifWritePrefer) {
        this.preferWrite = ifWritePrefer;
    }
}
